package com.gomo;

import java.util.ArrayList;
import org.ksoap2.serialization.SoapObject;
import com.gomo.adapter.CcAdapter;
import com.gomo.entity.Cc;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CcListActivity extends ListActivity {
	CcAdapter m_adapter;
	ArrayList<Cc> theArrayList;
	AlertDialog alertDialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cc_list);
		if (isValidate()) {
			new ParseXmlTaskGetCcList().execute();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.list_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_add_new:
			Intent myIntent = new Intent(getBaseContext(), CcAddActivity.class);
			startActivityForResult(myIntent, 0);
			// shareEmail();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private boolean isValidate() {
		/*if (!GlobalService.isOnline(this)) {
			GlobalService
					.MessageBox("Not or limited internet connection", this);
			return false;
		}*/
		
		//String hostUrl = getString(R.string.service_url);
		//if (!GlobalService.isHostAvailable(hostUrl)) {
		//	GlobalService.MessageBox("Web service not responding", this);
		//	return false;
		//}
		return true;
	}

	private ArrayList<Cc> processServiceGetCcList() {
		SoapService ss = new SoapService();

		String soap_method_name = getString(R.string.method_cc_get);
		String soap_namespace = getString(R.string.service_namespace);
		String soap_url = getString(R.string.service_url);
		String soap_action = soap_namespace + soap_method_name;
		String android_id = Secure.getString(getBaseContext()
				.getContentResolver(), Secure.ANDROID_ID);

		SoapObject soap_object_request = new SoapObject(soap_namespace,
				soap_method_name);

		soap_object_request.addProperty("UserId", GlobalService.getUserId());
		soap_object_request.addProperty("PinCode",
				GlobalService.getUserPinCode());
		soap_object_request.addProperty("DeviceId", android_id);
		soap_object_request.addProperty("DeviceType", "2");

		SoapObject soap_object_response = ss.parseWebService(soap_action,
				soap_url, soap_object_request);

		SoapObject recordsArray = (SoapObject) soap_object_response
				.getProperty(0);
		int recordCount = recordsArray.getPropertyCount();
		theArrayList = new ArrayList<Cc>();
		try {
			for (int i = 0; i < recordCount; i++) {
				Cc cc = new Cc();
				Ksoap2ResultParser.parseBusinessObject(recordsArray
						.getProperty(i).toString(), cc);
				theArrayList.add(cc);
			}
		}

		catch (NumberFormatException e) {
			// e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// e.printStackTrace();
		} catch (IllegalAccessException e) {
			// e.printStackTrace();
		} catch (InstantiationException e) {
			// e.printStackTrace();
		}
		return theArrayList;
	}

	class ParseXmlTaskGetCcList extends AsyncTask<Void, Void, ArrayList<Cc>> {
		private final ProgressDialog dialog = new ProgressDialog(
				CcListActivity.this);

		protected void onPreExecute() {
			this.dialog.setMessage("Getting credit card list...");
			this.dialog.show();
		}

		protected ArrayList<Cc> doInBackground(Void... unused) {
			return processServiceGetCcList();
		}

		protected void onPostExecute(ArrayList<Cc> cclist) {
			if (this.dialog.isShowing())
				this.dialog.dismiss();
			m_adapter = new CcAdapter(CcListActivity.this, cclist);
			theArrayList = cclist;
			setListAdapter(m_adapter);
		}
	}

}